package com.fishery.processing.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fishery.entity.GlobalException;
import com.fishery.entity.StatusCode;
import com.fishery.processing.client.OrderClient;
import com.fishery.processing.dao.CraftDao;
import com.fishery.processing.dao.FactoryDao;
import com.fishery.processing.dao.ProductDao;
import com.fishery.processing.entity.Craft;
import com.fishery.processing.entity.Factory;
import com.fishery.processing.entity.Product;
import com.fishery.processing.entity.client.OrderVo;
import com.fishery.processing.mapper.ProductMapper;
import com.fishery.processing.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private CraftDao craftDao;

    @Autowired
    private FactoryDao factoryDao;



    public int createProduct(Product product){
        List<Product> products = productDao.findProductOfFactoryAndName(product);
        if (!products.isEmpty()){
            throw new GlobalException(StatusCode.DUPLICATE,"该加工产品已存在，无法添加");
        }
        Factory factory = factoryDao.findFactoryById(product.getProcessingFactoryId());
        if (factory == null){
            throw new GlobalException(StatusCode.ERROR,"加工厂不存在,无法添加加工产品");
        }
        //id置空 等待mybatis-plus填充
        product.setId(null);
        return productDao.addProduct(product);
    }

    @Transactional
    public int deleteProduct(String productId){
        //删除加工产品与其工艺的连接信息
        craftDao.removeAllCraftOfProduct(productId);
        return productDao.removeProduct(productId);
    }

    public int updateProduct(Product product){
        List<Product> products = productDao.findProductOfFactoryAndName(product);
        if (!products.isEmpty()){
            if (!products.get(0).getId().equals(product.getId())){
                throw new GlobalException(StatusCode.DUPLICATE,"该加工产品名称已存在，无法修改");
            }
        }
        return productDao.updateProduct(product);
    }

    public Product searchProductById(String productId){
        return productDao.findProductById(productId);
    }

    //分页获取加工厂的产品列表
    public IPage<Product> searchProductOfFactory(String factoryId,Integer page,Integer size){
        return productDao.findProductOfFactory(factoryId,page,size);
    }

    //(全部)获取加工厂的产品列表
    public List<Product> searchProductOfFactory(String factoryId){
        return productDao.findProductOfFactory(factoryId);
    }

    //给加工产品添加工艺
    @Transactional
    public int addCraft(String productId,String craftId,Integer sort){
        long count = craftDao.getCraftCountOfProduct(productId);
        if (sort > count + 1 || sort <= 0){
            throw new GlobalException(StatusCode.ERROR,"工艺步骤不合法");
        }
        return craftDao.addCraftOfProduct(productId,craftId,sort);
    }

    //删除加工产品指定的工艺
    @Transactional
    public int deleteCraft(String productId,Integer sort){
        long count = craftDao.getCraftCountOfProduct(productId);
        if (sort > count || sort <= 0){
            throw new GlobalException(StatusCode.ERROR,"工艺步骤不合法");
        }
        return craftDao.removeCraftOfProduct(productId,sort);
    }

    //修改加工产品指定步骤的工艺
    public int updateCraft(String productId, String craftId, Integer sort) {
        long count = craftDao.getCraftCountOfProduct(productId);
        if (sort > count || sort <= 0){
            throw new GlobalException(StatusCode.ERROR,"工艺步骤不合法");
        }
        return craftDao.updateCraftOfProduct(productId,craftId,sort);
    }

}
